﻿using BLevy.Datos.Interfases;
using BLevy.Entidad;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using DapperExtensions;

namespace BLevy.Datos.Repositories
{
    public interface IFormaPagoRepository : IRepository<FormaPago>
    {

    }

    public class FormaPagoRepository : IFormaPagoRepository
    {
        protected readonly IDbConnection _db;

        public FormaPagoRepository()
        {
            try
            {
                _db = new MySqlConnection(ConfigurationManager.ConnectionStrings["LEVYDB"].ToString());
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public IList<FormaPago> FindAll()
        {
            try
            {
                using (IDbConnection _db = new MySqlConnection(ConfigurationManager.ConnectionStrings["LEVYDB"].ToString()))
                {
                    return _db.Query<FormaPago>("SELECT * FROM FORMASPAGO").ToList();
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public IQueryable<FormaPago> Find(System.Linq.Expressions.Expression<Func<FormaPago, bool>> predicate)
        {
            throw new NotImplementedException();
        }

        public FormaPago FindById(int id)
        {
            string query = "SELECT * FROM FORMASPAGO " +
                "WHERE ID = " + id;

            try
            {
                using (IDbConnection _db = new MySqlConnection(ConfigurationManager.ConnectionStrings["LEVYDB"].ToString()))
                {
                    return _db.Query<FormaPago>(query).SingleOrDefault();
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public int Add(FormaPago newEntity)
        {
            string query = "INSERT INTO FORMASPAGO (FORMA_PAGO, DEBITO_AUTOMATICO,ACTIVO) VALUES (@forma_pago, @debito_automatico, @activo)";

            try
            {
                using (IDbConnection _db = new MySqlConnection(ConfigurationManager.ConnectionStrings["LEVYDB"].ToString()))
                {
                    return _db.Execute(query, new
                    {
                        forma_pago = newEntity.forma_pago,
                        debito_automatico = newEntity.debito_automatico,
                        activo = newEntity.activo
                    });
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public int Remove(int id)
        {
            string query = "UPDATE FORMASPAGO SET ACTIVO = 0 " +
                "WHERE ID = @id";

            try
            {
                using (IDbConnection _db = new MySqlConnection(ConfigurationManager.ConnectionStrings["LEVYDB"].ToString()))
                {
                    return _db.Execute(query, new { id = id });
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public int Modify(FormaPago entity)
        {
            string query = "UPDATE FORMASPAGO SET FORMA_PAGO = @forma_pago, DEBITO_AUTOMATICO = @debito_automatico, ACTIVO = @activo " +
                "WHERE ID = @id";

            try
            {
                using (IDbConnection _db = new MySqlConnection(ConfigurationManager.ConnectionStrings["LEVYDB"].ToString()))
                {
                    return _db.Execute(query, new
                    {
                        forma_pago = entity.forma_pago,
                        debito_automatico = entity.debito_automatico,
                        activo = entity.activo,
                        id = entity.id
                    });
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }
    }
}
